using System;
using System.Web;
using System.Collections;
using System.Web.Services;
using System.Web.Services.Protocols;
using BadHabit.Factory;
using BadHabit.Factory.Data;
using BadHabit.Factory.Reports;
using System.Security.Principal;
using System.Security.Permissions;





/// <summary>
/// Summary description for WebService
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
public class ProjectService : System.Web.Services.WebService {

    public FactorySOAPHeader FactoryHeader;

    /// <summary>
    /// Constructor.
    /// </summary>
    public ProjectService() {

        string url = Context.Request.Url.Scheme + "://" + Context.Request.Url.Authority + "/";

        
        // lets make this a singleton
        // this way i only read the factory configuration once
        Factory.CreateFactory(this.ServiceFilePath, url);

             
        
    }

    // <summary>
    /// Add a new sprint to a given project.
    /// </summary>
    /// <param name="dsProject">The project dataset</param>
    /// <returns>The project with the new sprint</returns>
    [WebMethod, SoapHeader("FactoryHeader", Direction = SoapHeaderDirection.InOut)]
    [PrincipalPermission(SecurityAction.Demand, Role = "SCRUM_MASTER")]
    public ProjectDataSet RetriveSprintDefaultItems(string projectCategory) {
        return Factory.Current.RetriveSprintDefaultItems(projectCategory);
    }

    [WebMethod, SoapHeader("FactoryHeader", Direction = SoapHeaderDirection.InOut)]
    [PrincipalPermission(SecurityAction.Demand, Role = "TEAM")]
    public TeamDataSet RetriveAllMemberAllocation() {
        return Factory.Current.RetriveAllMemberAllocation();
    }

    #region General Methods

    

    /// <summary>
    /// Adds an amount of effort to a given task.
    /// </summary>
    /// <param name="taskUId">Task Uid</param>
    /// <param name="hours">Amount of hours</param>
    /// <param name="closeIt">True if the task should be closed</param>
    /// <returns>Any error message</returns>
    [WebMethod, SoapHeader("FactoryHeader", Direction = SoapHeaderDirection.InOut)]
    [PrincipalPermission(SecurityAction.Demand, Role = "TEAM")]
    public string ChangeTask(string oldTaskUId, double workedHours, bool closeIt, string newTaskUId) {
        return Factory.Current.ChangeTask(oldTaskUId, workedHours, closeIt, newTaskUId, Factory.Current.CurrentTeamMember);            
    }

    /// <summary>
    /// Adds an amount of effort to a given task.
    /// </summary>
    /// <param name="taskUId">Task Uid.</param>
    /// <param name="hours">Amount of hours</param>
    /// <param name="closeIt">True if the task should be closed</param>
    /// <returns>Any error message</returns>
    [WebMethod, SoapHeader("FactoryHeader", Direction = SoapHeaderDirection.InOut)]
    [PrincipalPermission(SecurityAction.Demand, Role = "TEAM")]
    public string AddsActualEffort(string taskUId, double hours, bool closeIt) {
        return Factory.Current.AddsActualEffort(taskUId, hours, closeIt);
    }
    [WebMethod, SoapHeader("FactoryHeader", Direction = SoapHeaderDirection.InOut)]
    public string GuideUrlForTemplate(string template, short status) {
        return string.Concat(new object[] { base.Context.Request.Url.GetLeftPart(UriPartial.Query).Replace("ProjectService.asmx", "").Replace("/GuideUrlForTemplate", ""), "/Templates/", template, "/Guide/", status, ".htm" });
    }

    [WebMethod, SoapHeader("FactoryHeader", Direction = SoapHeaderDirection.InOut)]
    [PrincipalPermission(SecurityAction.Demand, Role = "TEAM")]
    public string[] RetriveAllClients() {
        return Factory.Current.RetriveAllClients();
    }

    [WebMethod, SoapHeader("FactoryHeader", Direction = SoapHeaderDirection.InOut)]
    [PrincipalPermission(SecurityAction.Demand, Role = "TEAM")]
    public ProjectDataSet RetriveAllPayableProjects() {
        return Factory.Current.RetriveAllPayableProjects();
    }

    [WebMethod, SoapHeader("FactoryHeader", Direction = SoapHeaderDirection.InOut)]
    [PrincipalPermission(SecurityAction.Demand, Role = "TEAM")]
    public ProjectDataSet RetriveAllProjectModels() {
        return Factory.Current.RetriveAllProjectModels();
    }

    [WebMethod, SoapHeader("FactoryHeader", Direction = SoapHeaderDirection.InOut)]
    [PrincipalPermission(SecurityAction.Demand, Role = "TEAM")]
    public ProjectDataSet RetriveAllProjectPlataforms() {
        return Factory.Current.RetriveAllProjectPlataforms();
    }

    [WebMethod(BufferResponse = false), SoapHeader("FactoryHeader", Direction = SoapHeaderDirection.InOut)]
    [PrincipalPermission(SecurityAction.Demand, Role = "TEAM")]
    public ProjectDataSet RetriveAllProjects() {
        return Factory.Current.RetriveAllProjects();
    }

    
    

    [WebMethod, SoapHeader("FactoryHeader", Direction = SoapHeaderDirection.InOut)]
    [PrincipalPermission(SecurityAction.Demand, Role = "TEAM")]
    public ProjectDataSet RetriveTeamMemberProjects(string teamMemberId) {
        return Factory.Current.RetriveTeamMemberProjects(teamMemberId);
    }

    [WebMethod, SoapHeader("FactoryHeader", Direction = SoapHeaderDirection.InOut)]
    [PrincipalPermission(SecurityAction.Demand, Role = "TEAM")]
    public ProjectDataSet RetriveRunningProjects() {
        return Factory.Current.RetriveRunningProjects();
    }

    [WebMethod, SoapHeader("FactoryHeader", Direction = SoapHeaderDirection.InOut)]
    [PrincipalPermission(SecurityAction.Demand, Role = "TEAM")]
    public ProjectDataSet RetriveScheduledProjects() {
        return Factory.Current.RetriveScheduledProjects();
    }

    [WebMethod, SoapHeader("FactoryHeader", Direction = SoapHeaderDirection.InOut)]
    [PrincipalPermission(SecurityAction.Demand, Role = "TEAM")]
    public ProjectDataSet RetriveToDoProjectsProjects(string userUId) {
        return Factory.Current.RetriveToDoProjects(userUId);
    }

 

    [WebMethod, SoapHeader("FactoryHeader", Direction = SoapHeaderDirection.InOut)]
    [PrincipalPermission(SecurityAction.Demand, Role = "TEAM")]
    public TeamDataSet RetriveMembersByCompany(string ownerCompany) {
        return Factory.Current.RetriveMembersByCompany(ownerCompany);
    }

    [WebMethod, SoapHeader("FactoryHeader", Direction = SoapHeaderDirection.InOut)]
    [PrincipalPermission(SecurityAction.Demand, Role = "TEAM")]
    public bool IsReadingClientContactsFromMsExchange() {
        return Factory.Current.IsReadingClientContactsFromMsExchange;
    }

   


    [WebMethod(BufferResponse = false), SoapHeader("FactoryHeader", Direction = SoapHeaderDirection.InOut)]
    [PrincipalPermission(SecurityAction.Demand, Role = "TEAM")]
    public ProjectDataSet RetriveClosedProjects() {
        return Factory.Current.RetriveClosedProjects();
    }

    [WebMethod, SoapHeader("FactoryHeader", Direction = SoapHeaderDirection.InOut)]
    [PrincipalPermission(SecurityAction.Demand, Role = "TEAM")]
    public ProjectDataSet RetriveOportunitiesProjects() {
        return Factory.Current.RetriveOportunitiesProjects();
    }


    #endregion

    #region Commercial Methods
    
   


    /// <summary>
    /// Retrive still to pay projects.
    /// </summary>        
    /// <returns>project dataset</returns>
    [WebMethod, SoapHeader("FactoryHeader", Direction = SoapHeaderDirection.InOut)]
    [PrincipalPermission(SecurityAction.Demand, Role = "COMMERCIAL_GUY")]
    [PrincipalPermission(SecurityAction.Demand, Role = "FACTORY_OWNER")]
    public BadHabit.Factory.Data.ProjectDataSet RetriveStillToPayProjects() {
        return Factory.Current.RetriveStillToPayProjects();
    }

    /// <summary>
    /// Retrive in debit projects.
    /// </summary>        
    /// <returns>project dataset</returns>
    [WebMethod, SoapHeader("FactoryHeader", Direction = SoapHeaderDirection.InOut)]
    [PrincipalPermission(SecurityAction.Demand, Role = "COMMERCIAL_GUY")]
    [PrincipalPermission(SecurityAction.Demand, Role = "FACTORY_OWNER")]
    public BadHabit.Factory.Data.ProjectDataSet RetriveInDebitProjects() {
        return Factory.Current.RetriveInDebitProjects();
    }

    [WebMethod, SoapHeader("FactoryHeader", Direction = SoapHeaderDirection.InOut)]
    [PrincipalPermission(SecurityAction.Demand, Role = "COMMERCIAL_GUY")]
    [PrincipalPermission(SecurityAction.Demand, Role = "FACTORY_OWNER")]
    public ProjectDataSet RetriveProposalTemplate(string template) {
        return Factory.Current.RetriveProposalTemplate(this.ServiceFilePath + @".\App_Data\ProjectTemplates\" + template);
    }

    [WebMethod, SoapHeader("FactoryHeader", Direction = SoapHeaderDirection.InOut)]
    [PrincipalPermission(SecurityAction.Demand, Role = "COMMERCIAL_GUY")]
    [PrincipalPermission(SecurityAction.Demand, Role = "FACTORY_OWNER")]
    public string[] RetriveProposalTemplateList() {
        ProposalReport report = new ProposalReport(Factory.Current, null);
        return report.RetriveTemplateList();
    }


    
    /// <summary>
    /// Returns the proposal xaml.
    /// </summary>
    /// <param name="project">Project Dataset</param>
    /// <param name="templateName">Template Name</param>
    /// <returns>File DataSet</returns>   
    [WebMethod(BufferResponse = false), SoapHeader("FactoryHeader", Direction = SoapHeaderDirection.InOut)]
    [PrincipalPermission(SecurityAction.Demand, Role = "COMMERCIAL_GUY")]
    [PrincipalPermission(SecurityAction.Demand, Role = "FACTORY_OWNER")]
    public string GenerateProposalXAML(ProjectDataSet project, string proposalUId, string templateName) {

        string approvedProposalXaml = Factory.Current.RetriveApprovedProposalXaml(proposalUId);
        if (approvedProposalXaml != null)
            return approvedProposalXaml;

        ProposalReport report = new ProposalReport(Factory.Current, proposalUId);      
        return report.GenerateFlowDocumentXAML(project, templateName);        
    }



    [WebMethod(BufferResponse = false), SoapHeader("FactoryHeader", Direction = SoapHeaderDirection.InOut)]
    [PrincipalPermission(SecurityAction.Demand, Role = "TEAM")]
    public string GenerateTeamReportXAML(int month, int year) {
        ResourceReport report = new ResourceReport(Factory.Current, month, year);
        return report.GenerateFlowDocumentXAML("ResourceReport.xslt");
    }
        
    /// <summary>
    /// Aproves a given proposal for a project.
    /// </summary>
    /// <param name="project">The project</param>
    /// <param name="proposalUId">The proposal</param>
    /// <returns>Any error message</returns>
    [WebMethod, SoapHeader("FactoryHeader", Direction = SoapHeaderDirection.InOut)]
    [PrincipalPermission(SecurityAction.Demand, Role = "COMMERCIAL_GUY")]
    [PrincipalPermission(SecurityAction.Demand, Role = "FACTORY_OWNER")]
    public ProjectDataSet ApproveProposal(ProjectDataSet project, string proposalUId, out string alertMessage) {
        return Factory.Current.ApproveProposal(project, proposalUId, out alertMessage);
    }

    [WebMethod, SoapHeader("FactoryHeader", Direction = SoapHeaderDirection.InOut)]
    [PrincipalPermission(SecurityAction.Demand, Role = "COMMERCIAL_GUY")]
    [PrincipalPermission(SecurityAction.Demand, Role = "FACTORY_OWNER")]
    public ProjectDataSet RejectProposal(ProjectDataSet project, string proposalUId, out string alertMessage) {
        return Factory.Current.RejectProposal(project, proposalUId, out alertMessage);
    }

    #endregion

    #region Project Methods

    


    [WebMethod, SoapHeader("FactoryHeader", Direction = SoapHeaderDirection.InOut)]
    [PrincipalPermission(SecurityAction.Demand, Role = "SCRUM_MASTER")]
    [PrincipalPermission(SecurityAction.Demand, Role = "QUALITY_ASSURANCE")]
    public ProjectDataSet SaveProject(ProjectDataSet project, out string alertMessage) {
        return Factory.Current.PrepareToSaveProject(project, out alertMessage);
    }



 

    #endregion

    #region Report Methods

    [WebMethod(BufferResponse = false), SoapHeader("FactoryHeader", Direction = SoapHeaderDirection.InOut)]
    [PrincipalPermission(SecurityAction.Demand, Role = "TEAM")]
    public string GenerateTailReportXAML(string projectUId, string templateName) {        
        TailReport report = new TailReport(Factory.Current);
        report.ProjectUId = projectUId;
        return report.GenerateFlowDocumentXAML(templateName);
    }

    [WebMethod(BufferResponse = false), SoapHeader("FactoryHeader", Direction = SoapHeaderDirection.InOut)]
    [PrincipalPermission(SecurityAction.Demand, Role = "TEAM")]
    public string GenerateHourBankReportXAML(string projectUId, string templateName) {
        if ((templateName == null) || (templateName == "")) {
            templateName = "Default.xslt";
        }
        HourBankReport report = new HourBankReport(Factory.Current);
        report.ProjectUId = projectUId;
        return report.GenerateFlowDocumentXAML(templateName);
    }





    [WebMethod(BufferResponse = false), SoapHeader("FactoryHeader", Direction = SoapHeaderDirection.InOut)]
    [PrincipalPermission(SecurityAction.Demand, Role = "TEAM")]
    public string GenerateBackLogReportXAML(string projectUId, string templateName) {
        if ((templateName == null) || (templateName == "")) {
            templateName = "Standart Backlog.xslt";
        }
        BacklogReport report = new BacklogReport(Factory.Current);
        report.ProjectUId = projectUId;
        return report.GenerateFlowDocumentXAML(templateName);
    }
           
    [WebMethod, SoapHeader("FactoryHeader", Direction = SoapHeaderDirection.InOut)]
    [PrincipalPermission(SecurityAction.Demand, Role = "TEAM")]
    public EmailDataSet RetriveHourBankReportEmail(string projectUId) {
        return Factory.Current.RetriveTemplateEmail(projectUId, @"\Proposal\HourBankReportEmail.xml");
    }

    [WebMethod, SoapHeader("FactoryHeader", Direction = SoapHeaderDirection.InOut)]
    [PrincipalPermission(SecurityAction.Demand, Role = "TEAM")]
    public string[] RetriveHourBankReportTemplateList() {
        HourBankReport report = new HourBankReport(Factory.Current);        
        return report.RetriveTemplateList();
    }

    [WebMethod, SoapHeader("FactoryHeader", Direction = SoapHeaderDirection.InOut)]
    [PrincipalPermission(SecurityAction.Demand, Role = "TEAM")]
    public string[] RetriveTailReportTemplateList() {
        TailReport report = new TailReport(Factory.Current);
        return report.RetriveTemplateList();
    }

    [WebMethod, SoapHeader("FactoryHeader", Direction = SoapHeaderDirection.InOut)]
    [PrincipalPermission(SecurityAction.Demand, Role = "TEAM")]
    public string[] RetriveBacklogReportTemplateList() {
        BacklogReport report = new BacklogReport(Factory.Current);
        return report.RetriveTemplateList();
    }

    #endregion

    #region Task Methods

    [WebMethod, SoapHeader("FactoryHeader", Direction = SoapHeaderDirection.InOut)]
    [PrincipalPermission(SecurityAction.Demand, Role = "TEAM")]
    public TaskDataSet RetriveTasksByTailedBacklogItemUId(string backlogItemUId) {
        return Factory.Current.RetriveTasksByTailedBacklogItemUId(backlogItemUId);
    }

    [WebMethod, SoapHeader("FactoryHeader", Direction = SoapHeaderDirection.InOut)]
    [PrincipalPermission(SecurityAction.Demand, Role = "TEAM")]
    public TaskDataSet RetriveTasksByTailedProjectUId(string projectUId) {
        return Factory.Current.RetriveTasksByTailedProjectUId(projectUId);
    }

    [WebMethod, SoapHeader("FactoryHeader", Direction = SoapHeaderDirection.InOut)]
    [PrincipalPermission(SecurityAction.Demand, Role = "TEAM")]
    public TaskDataSet RetriveTasksByTailUId(string tailUId) {
        return Factory.Current.RetriveTasksByTailUId(tailUId);
    }

    [WebMethod, SoapHeader("FactoryHeader", Direction = SoapHeaderDirection.InOut)]
    [PrincipalPermission(SecurityAction.Demand, Role = "TEAM")]
    public TaskDataSet RetriveTasksByArtifactPath(string artifactPath) {
        return Factory.Current.RetriveTasksByArtifactPath(artifactPath);
    }

    [WebMethod, SoapHeader("FactoryHeader", Direction = SoapHeaderDirection.InOut)]
    [PrincipalPermission(SecurityAction.Demand, Role = "TEAM")]
    public System.Data.DataSet RetriveOpenedTasks() {
        return Factory.Current.RetriveOpenedTasks();
    }

    [WebMethod(BufferResponse = false), SoapHeader("FactoryHeader", Direction = SoapHeaderDirection.InOut)]    
    [PrincipalPermission(SecurityAction.Demand, Role = "TEAM")]
    public TaskDataSet RetriveAllTasks() {
        return Factory.Current.RetriveAllTasks();
    }

    [WebMethod(BufferResponse = false), SoapHeader("FactoryHeader", Direction = SoapHeaderDirection.InOut)]
    [PrincipalPermission(SecurityAction.Demand, Role = "TEAM")]
    public TaskDataSet RetriveProjectTasks(string projectUId, short releaseNumber) {
        return Factory.Current.RetriveProjectTasks(projectUId, releaseNumber);
    }

    [WebMethod, SoapHeader("FactoryHeader", Direction = SoapHeaderDirection.InOut)]
    [PrincipalPermission(SecurityAction.Demand, Role = "TEAM")]
    public TaskDataSet RetriveProjectDaillyTasks(string projectUId) {
        return Factory.Current.RetriveDaillyProjectTasks(projectUId);
    }

    [WebMethod(BufferResponse = false), SoapHeader("FactoryHeader", Direction = SoapHeaderDirection.InOut)]
    [PrincipalPermission(SecurityAction.Demand, Role = "TEAM")]
    public TaskDataSet RetriveClosedProjectTasks(string projectUId, short releaseNumber) {
        return Factory.Current.RetriveClosedProjectTasks(projectUId, releaseNumber);
    }

    [WebMethod, SoapHeader("FactoryHeader", Direction = SoapHeaderDirection.InOut)]
    [PrincipalPermission(SecurityAction.Demand, Role = "TEAM")]
    public TaskDataSet.TaskResumeDataTable RetriveOpenedAssignedTasks(string taskAssignee) {
        return Factory.Current.RetriveOpenedAssignedTasks(taskAssignee);
    }

    [WebMethod, SoapHeader("FactoryHeader", Direction = SoapHeaderDirection.InOut)]
    [PrincipalPermission(SecurityAction.Demand, Role = "TEAM")]
    public TaskDataSet RetriveOpenedProjectTasks(string projectUId, short releaseNumber) {
        return Factory.Current.RetriveOpenedProjectTasks(projectUId, releaseNumber);
    }

    [WebMethod, SoapHeader("FactoryHeader", Direction = SoapHeaderDirection.InOut)]
    [PrincipalPermission(SecurityAction.Demand, Role = "TEAM")]
    public TaskDataSet RetriveOpenedProjectTasksForUser(string projectUId, string taskAssignee, short releaseNumber) {
        return Factory.Current.RetriveOpenedProjectTasksForUser(projectUId, taskAssignee, releaseNumber);
    }

    #endregion    


    #region Tail Methods

    [WebMethod, SoapHeader("FactoryHeader", Direction = SoapHeaderDirection.InOut)]
    /// <summary>
    /// Creates a tail between a given task and a group of artifacts.
    /// </summary>
    /// <param name="taskId">The taskId</param>
    /// <param name="artifactsPath">The list of artifact to tail to the task</param>
    public TaskDataSet TailTaskById(string repositoryUrl, int taskId, string[] artifactsPath, int revision) {
        return Factory.Current.TailTaskById(repositoryUrl, taskId, artifactsPath, revision);
    }

    
    [WebMethod, SoapHeader("FactoryHeader", Direction = SoapHeaderDirection.InOut)]
    public string RetriveTeamMemberWorkingTaskUId(string teamMember) {
        return Factory.Current.GetWorkingTaskUIdByTeamMember(teamMember);
    }

    [WebMethod, SoapHeader("FactoryHeader", Direction = SoapHeaderDirection.InOut)]
    /// <summary>
    /// Creates a tail between current working task of a team member and a group of artifacts.
    /// </summary>
    /// <param name="teamMember">The team member</param>
    /// <param name="artifactsPath">The list of artifact to tail to the task</param>
    public TaskDataSet TailTaskByTeamMember(string repositoryUrl, string teamMember, string[] artifactsPath, int revision) {
        return Factory.Current.TailTaskByTeamMember(repositoryUrl, teamMember, artifactsPath, revision);
    }

    [WebMethod, SoapHeader("FactoryHeader", Direction = SoapHeaderDirection.InOut)]
    [PrincipalPermission(SecurityAction.Demand, Role = "TEAM")]
    /// <summary>
    /// Removes a task tail.
    /// </summary>    
    public void RemoveTaskTail(string taskTailUId) {
        Factory.Current.DeleteTaskTail(taskTailUId);
    }

    [WebMethod, SoapHeader("FactoryHeader", Direction = SoapHeaderDirection.InOut)]
    [PrincipalPermission(SecurityAction.Demand, Role = "TEAM")]
    /// <summary>
    /// Removes a task tail.
    /// </summary>    
    public void RemoveTaskTailFromBacklogItem(string artifactPath, string backlogItemUId) {
        Factory.Current.DeleteTaskTail(backlogItemUId, artifactPath);
    }

   

    [WebMethod, SoapHeader("FactoryHeader", Direction = SoapHeaderDirection.InOut)]
    [PrincipalPermission(SecurityAction.Demand, Role = "TEAM")]
    /// <summary>
    /// Removes a task tail.
    /// </summary>    
    public void TailTaskMaterialAsLink(string taskUId, string[] materialPath) {
        Factory.Current.TailTaskMaterial(taskUId, materialPath);
    }

    #endregion


    #region Team Member Methos

    [WebMethod, SoapHeader("FactoryHeader", Direction = SoapHeaderDirection.InOut)]
    public void RefreshTeamMembers() {
        Factory.Current.LoadTeamMembers();
    }

    [WebMethod, SoapHeader("FactoryHeader", Direction = SoapHeaderDirection.InOut)]
    [PrincipalPermission(SecurityAction.Demand, Role = "TEAM")]
    public void UpdateTeamMemberImage(Byte[] imageBytes, string memberId) {
        Factory.Current.UpdateTeamMemberImage(imageBytes, memberId);
    }

    [WebMethod, SoapHeader("FactoryHeader", Direction = SoapHeaderDirection.InOut)]
    [PrincipalPermission(SecurityAction.Demand, Role = "TEAM")]
    public void UpdateMyTeamMemberImage(Byte[] imageBytes) {
        Factory.Current.UpdateTeamMemberImage(imageBytes);
    }


    [WebMethod, SoapHeader("FactoryHeader", Direction = SoapHeaderDirection.InOut)]
    [PrincipalPermission(SecurityAction.Demand, Role = "TEAM")]
    public BadHabit.Factory.Data.TeamDataSet RetriveMembersAtSameProject(string teamMemberId) {
        return Factory.Current.RetriveMembersAtSameProject(teamMemberId);
    }

    [WebMethod, SoapHeader("FactoryHeader", Direction = SoapHeaderDirection.InOut)]
    [PrincipalPermission(SecurityAction.Demand, Role = "TEAM")]
    public TeamDataSet RetriveAllTeamMembers() {
        return Factory.Current.GetTeamMembers();
    }

    [WebMethod, SoapHeader("FactoryHeader", Direction = SoapHeaderDirection.InOut)]
    [PrincipalPermission(SecurityAction.Demand, Role = "FACTORY_OWNER")]
    public void SaveTeamMemberList(TeamDataSet teamDataSet) {
        Factory.Current.SaveTeamMemberList(teamDataSet);
    }


    #endregion


    [WebMethod, SoapHeader("FactoryHeader", Direction = SoapHeaderDirection.InOut)]
    [PrincipalPermission(SecurityAction.Demand, Role = "TEAM")]
    public void SaveTeamMemberProfile(TeamDataSet dsTeam) {
        Factory.Current.SaveTeamMemberProfile(dsTeam);
    }

    [WebMethod, SoapHeader("FactoryHeader", Direction = SoapHeaderDirection.InOut)]
    [PrincipalPermission(SecurityAction.Demand, Role = "TEAM")]
    public TaskDataSet RetriveAverageHoursBySizeItemType() {
        return Factory.Current.RetriveAverageHoursBySizeItemType();
    }


    [WebMethod, SoapHeader("FactoryHeader", Direction=SoapHeaderDirection.InOut)]    
    public ProjectDataSet RetriveProject(string projectUId)   {
        return Factory.Current.RetriveProject(projectUId);
    }

    [WebMethod, SoapHeader("FactoryHeader", Direction = SoapHeaderDirection.InOut)]
    public ProjectDataSet RetriveProjectAndRolesOnly(string projectUId) {
        return Factory.Current.RetriveProjectAndRolesOnly(projectUId);
    }

    [WebMethod, SoapHeader("FactoryHeader", Direction = SoapHeaderDirection.InOut)]
    public ProjectDataSet RetriveProjectByNumber(int projectNumber) {
        return Factory.Current.RetriveProject(projectNumber);
    }

    
       

    [WebMethod, SoapHeader("FactoryHeader", Direction=SoapHeaderDirection.InOut)]
    [PrincipalPermission(SecurityAction.Demand, Role = "SCRUM_MASTER")]
    [PrincipalPermission(SecurityAction.Demand, Role = "FACTORY_OWNER")]
    public ProjectDataSet RetriveProjectTemplate(string template) {
        return Factory.Current.RetriveProjectTemplate(this.ServiceFilePath + @".\App_Data\ProjectTemplates\" + template);
    }
    
    

    [WebMethod, SoapHeader("FactoryHeader", Direction=SoapHeaderDirection.InOut)]    
    [PrincipalPermission(SecurityAction.Demand, Role = "TEAM")]
    public string[] RetriveProjectTemplateList() {
        return Factory.Current.RetriveProjectTemplateList(this.ServiceFilePath + @".\App_Data\ProjectTemplates");
    }

    [WebMethod, SoapHeader("FactoryHeader", Direction=SoapHeaderDirection.InOut)]
    [PrincipalPermission(SecurityAction.Demand, Role = "COMMERCIAL_GUY")]
    [PrincipalPermission(SecurityAction.Demand, Role = "FACTORY_OWNER")]
    public EmailDataSet RetriveProposalEmail(string projectUId) {
        return Factory.Current.RetriveTemplateEmail(projectUId, @"\Proposal\ProposalEmail.xml");
    }


    [WebMethod, SoapHeader("FactoryHeader", Direction=SoapHeaderDirection.InOut)]
    [PrincipalPermission(SecurityAction.Demand, Role = "TEAM")]
    public double RetriveUserActualMonthEffort(string taskAssignee, int month, int year) {
        return Factory.Current.RetriveUserActualMonthEffort(taskAssignee, month, year);
    }

    [WebMethod, SoapHeader("FactoryHeader", Direction=SoapHeaderDirection.InOut)]    
    public string[] RetriveTeamMemberRoles() {
        System.Collections.Generic.List<string> rolesList = new System.Collections.Generic.List<string>();

        foreach (string role in Enum.GetNames(typeof(TeamRoles))) {
            if (Factory.Current.CurrentTeamMemberPrincipal.IsInRole(role))
                rolesList.Add(role);

        }
        return rolesList.ToArray();
    }

    [WebMethod, SoapHeader("FactoryHeader", Direction=SoapHeaderDirection.InOut)]
    [PrincipalPermission(SecurityAction.Demand, Role = "TEAM")]
    public double RetriveUserPlannedEffort(string taskAssignee) {
        return Factory.Current.RetriveUserPlannedEffort(taskAssignee);
    }
          
    [WebMethod, SoapHeader("FactoryHeader", Direction = SoapHeaderDirection.InOut)]
    [PrincipalPermission(SecurityAction.Demand, Role = "TEAM")]
    public TaskDataSet SaveTask(TaskDataSet task, out string alertMessage) {
        return Factory.Current.SaveTask(task, out alertMessage);
    }

    [WebMethod, SoapHeader("FactoryHeader", Direction = SoapHeaderDirection.InOut)]
    [PrincipalPermission(SecurityAction.Demand, Role = "TEAM")]
    public TaskDataSet RetriveTask(int taskId) {
        return Factory.Current.RetriveTask(taskId);
    }
  

    [WebMethod, SoapHeader("FactoryHeader", Direction=SoapHeaderDirection.InOut)]
    [PrincipalPermission(SecurityAction.Demand, Role = "TEAM")]
    public string ValidateTask(TaskDataSet task) {
        string message = null;
        try {
            Factory.Current.ValidateTask(task);
        }
        catch (Exception exception) {
            message = exception.Message;
        }
        return message;
    }

    [WebMethod, SoapHeader("FactoryHeader", Direction=SoapHeaderDirection.InOut)]    
    public string CurrentTeamMember() {
        return Factory.Current.CurrentTeamMember;
    }

    // Properties
    private string ServiceFilePath {
        get {
            return base.Context.Request.PhysicalPath.Substring(0, base.Context.Request.PhysicalPath.LastIndexOf(@"\"));
        }
    }

    /// <summary>
    /// Retrive the factory team velocity for a given month.
    /// </summary>
    /// <param name="date">month</param>
    /// <returns>The velocity for the given month</returns>
    [WebMethod, SoapHeader("FactoryHeader", Direction=SoapHeaderDirection.InOut)]
    [PrincipalPermission(SecurityAction.Demand, Role = "TEAM")]
    public double RetriveFactoryTeamVelocity() {
        return Factory.Current.RetriveFactoryTeamVelocity();
    }

    [WebMethod, SoapHeader("FactoryHeader", Direction=SoapHeaderDirection.InOut)]
    [PrincipalPermission(SecurityAction.Demand, Role = "TEAM")]
    public ProjectDataSet RetriveBacklogItemUnits() {
        return Factory.Current.RetriveBacklogItemUnits();
    }


    
    [WebMethod, SoapHeader("FactoryHeader", Direction = SoapHeaderDirection.InOut)]
    [PrincipalPermission(SecurityAction.Demand, Role = "TEAM")]
    public TaskDataSet RetriveArtifactsByTaskUId(string taskUId) {
        return Factory.Current.RetriveArtifactsByTaskUId(taskUId);
    }

    [WebMethod, SoapHeader("FactoryHeader", Direction = SoapHeaderDirection.InOut)]
    [PrincipalPermission(SecurityAction.Demand, Role = "TEAM")]
    public TaskDataSet RetriveArtifactsByBacklogItemUId(string backlogItemUId, string projectCategory) {
        return Factory.Current.RetriveArtifactsByBacklogItemUId(backlogItemUId, projectCategory);
    }

    [WebMethod, SoapHeader("FactoryHeader", Direction = SoapHeaderDirection.InOut)]
    [PrincipalPermission(SecurityAction.Demand, Role = "TEAM")]
    public TaskDataSet RetriveArtifactsByProjectUId(string projectUId, string projectCategory) {
        return Factory.Current.RetriveArtifactsByProjectUId(projectUId, projectCategory);
    }

    [WebMethod, SoapHeader("FactoryHeader", Direction = SoapHeaderDirection.InOut)]
    [PrincipalPermission(SecurityAction.Demand, Role = "TEAM")]
    public TaskDataSet RetriveImpactedItemsByTaskUId(string taskUId) {
        return Factory.Current.RetriveImpactedItemsByTaskUId(taskUId);
    }

    [WebMethod, SoapHeader("FactoryHeader", Direction = SoapHeaderDirection.InOut)]
    [PrincipalPermission(SecurityAction.Demand, Role = "TEAM")]
    public TaskDataSet RetriveImpactedItemsByBacklogItemUId(string backlogItemUId) {
        return Factory.Current.RetriveImpactedItemsByBacklogItemUId(backlogItemUId);
    }

    [WebMethod, SoapHeader("FactoryHeader", Direction = SoapHeaderDirection.InOut)]
    [PrincipalPermission(SecurityAction.Demand, Role = "TEAM")]
    public TaskDataSet RetriveImpactedItemsByProjectUId(string projectUId) {
        return Factory.Current.RetriveImpactedItemsByProjectUId(projectUId);
    }

    [WebMethod, SoapHeader("FactoryHeader", Direction = SoapHeaderDirection.InOut)]
    [PrincipalPermission(SecurityAction.Demand, Role = "TEAM")]
    public TaskDataSet RetriveImpactedItemsByTailUId(string tailUId) {
        return Factory.Current.RetriveImpactedItemsByTailUId(tailUId);
    }

    [WebMethod, SoapHeader("FactoryHeader", Direction = SoapHeaderDirection.InOut)]
    [PrincipalPermission(SecurityAction.Demand, Role = "TEAM")]
    public ProjectDataSet RetriveBacklogItemSizes() {
        return Factory.Current.RetriveBacklogItemSizes();
    }

    [WebMethod, SoapHeader("FactoryHeader", Direction = SoapHeaderDirection.InOut)]
    [PrincipalPermission(SecurityAction.Demand, Role = "TEAM")]
    public void RetriveProjectIndicators(string projectUId, ref double budgetIndicator, ref double qualityIndicator, ref double velocityIndicator, ref double deadlineIndicator, ref double sizeIndicator, ref double incomeIndicator, ref double walkedIndicator) {

        
        BadHabit.Factory.Data.ProjectPerformanceDataSet.ProjectPerformanceRow ind = Factory.Current.RetriveProjectPerformanceIndicators(projectUId);
        if (ind == null)
            return;

        budgetIndicator = ind.PctBudgetBurned;
        qualityIndicator = ind.PctBugEffort;
        velocityIndicator = ind.Velocity;
        deadlineIndicator = ind.HoursAhead;
        incomeIndicator = ind.IncomeRank;
        sizeIndicator = ind.SiteRank;
        walkedIndicator = ind.PctWalked;
    }

    /// <summary>
    /// Returns the proposal xaml.
    /// </summary>
    /// <param name="project">Project Dataset</param>
    /// <param name="templateName">Template Name</param>
    /// <returns>File DataSet</returns>   
    [WebMethod(BufferResponse = false), SoapHeader("FactoryHeader", Direction = SoapHeaderDirection.InOut)]
    [PrincipalPermission(SecurityAction.Demand, Role = "TEAM")]    
    public string GenerateProjectPerformanceReportXAML(int year, string member, string template) {
        ProjectPerformanceReport report;
        if(member==null)
            report = new ProjectPerformanceReport(Factory.Current, year);
        else 
            report = new ProjectPerformanceReport(Factory.Current, year, member);

        return report.GenerateFlowDocumentXAML(template);
    }

    [WebMethod, SoapHeader("FactoryHeader", Direction = SoapHeaderDirection.InOut)]
    [PrincipalPermission(SecurityAction.Demand, Role = "TEAM")]
    public string[] RetriveProjectPerformanceReportTemplateList() {
        ProjectPerformanceReport report = new ProjectPerformanceReport(Factory.Current, 0);
        return report.RetriveTemplateList();
    }


    [WebMethod, SoapHeader("FactoryHeader", Direction = SoapHeaderDirection.InOut)]
    [PrincipalPermission(SecurityAction.Demand, Role = "TEAM")]
    public string RetriveSourceControlViewUrl() {
        return Factory.Current.SourceControlViewUrl;
    }
    

}
